/**
 * Created by xiaoz on 16/8/13.
 */
define(["utils", "base"], function (utils, baseClass) {
    var Component = function (config) {
        var _this = this;
        Component.baseConstructor.call(this, config);
        _this.config.style = _this.config.style || {};
        this.title = this.config.title || "";
        _this.config.imgStyle = _this.config.imgStyle || {};
        var className = _this.config.className ? "yy-image " + _this.config.className : "yy-image";
        this.src = config.src || config.defaultSrc;
        var width = this.config.style.width;
        try {
            width = parseInt(width);
        } catch (e) {
            width = null;
        }
        var height = this.config.style.height;
        try {
            height = parseInt(height);
        } catch (e) {
            height = null;
        }
        this.img = new Image();
        var preErrorImagesrc = "";
        // _this.img.style["display"] = "none";
        this.img.onload = function (e) {
            var style = {"display": "block"};
            if(_this.config.needAdapte){
                if(_this.img.width>_this.img.height){
                  style.width = "auto";
                  style.height = "100%";
                }else{
                  style.width = "100%";
                  style.height = "auto";

                }
                // style.width = size.w;
                // style.height = size.h;
            }else{
              style.width = "100%";
              style.height = "100%";
            }

            if (_this.config.style.borderRadius) {
                style.borderRadius = _this.config.style.borderRadius;
            }
            $(_this.img).css(style);
            _this.$el.append(_this.img);
        };
        this.img.src = this.src;

        var imgOnErrorMethodName = this.config.comKey +"_error";
        var imgOnErrorMethod = this.pageview.plugin[imgOnErrorMethodName];
        this.img.onerror = function () {
            if(imgOnErrorMethod){
              imgOnErrorMethod.call(_this.pageview.plugin,_this,{});
            }

            if (config.defaultSrc) {
                if (preErrorImagesrc === config.defaultSrc) {
                    _this.img.classList.add("displaynone");
                    return;
                }
                preErrorImagesrc = config.defaultSrc;
                _this.img.src = config.defaultSrc;
                return;
            }
            if (_this.title.length > 0) {
                var title = utils.getImgTitle(_this.title);
                _this.$el.html(title);
                _this.$el.css({
                    color: "#fff",
                    fontSize:(_this.config.style.fontSize || "13px"),
                    backgroundColor: utils.getImgBg(title)
                });
                return;
            }


        };
        this.config.imgStyle.height = "100%";
        this.config.imgStyle.width = "100%";

        $(_this.img).css(this.config.imgStyle);

        this.$el.addClass(className);
    }

    utils.extendClass(Component, baseClass);

    Component.prototype.setSrc = function (src) {
        this.img.src = src;
    };

    Component.prototype.setTitle = function (title) {
        var _this = this;
        var _title = utils.getImgTitle(title);
        this.$el.html(_title);
        this.$el.css({
            color: "#fff",
            fontSize: (_this.config.style.fontSize || "13px"),
            backgroundColor: utils.getImgBg(_title)
        });
    }

    return Component;
});
